// pages/address/address.js
import utils from "../../utils/utils.js";
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		tabinfo:[{'name':'个人','id':0},{'name':'公司','id':1}],
		tabindex:0,/* 个人和公司下标 */
		bindpersona:true,/* 隐藏公司名称和地址 */
		type:0,
		username: '请输入绑定姓名',
		phone: "请输入绑定手机号码",
		gaddress: "请输入公司地址",
		gname: "请输入公司名称",
		email: '2665644567@qq.com',
		birthday: '请选择出生日期',
		telephonenumber: '29292929',
		isDisabled: false,
		isComplete: false,
		buttonTitle: "完成",
		updateAddr: '',
		/* 提交按钮事件 */
		submit: '',
		date: '1980-09-01',
		getUserInfo: 'getUserInfo',
		k7: [{
				'arr_guige02': '男',
				'guige_key02': 0,
				'checked': true
			},
			{
				'arr_guige02': '女',
				'guige_key02': 1,
				'checked': false
			},
		],
		sex: '男',
		userInfo: {},
		hasUserInfo: false,
		canIUse: my.canIUse('button.open-type.getUserInfo')
	},
	tabselect(e){
		let that=this;
		let id=e.currentTarget.dataset.id;
		let bindpersona;
		if(id==0){
			bindpersona=true;
			that.data.type=0;
		}else{
			bindpersona=false;
			that.data.type=1;
		}
		this.setData({
			tabindex:id,
			bindpersona:bindpersona
		})
	},
	radio(e) {
		this.setData({
			guige_key02: e.currentTarget.dataset.id
		})
	},
	// 发货地址选择,获取用户选择的单选框的值
	radioChange(e) {
		this.data.sex = e.detail.value;
	},
	bindDateChange(e) {
		this.data.birthday = e.detail.value;
		this.setData({
			birthday: e.detail.value
		})
	},
	/* 绑定个人信息 */
	formSubmit(e) {
		let that = this;
		let userInfo = my.getStorageSync({key:'userInfo'});
		let type=that.data.type;
		//let birthday=this.data.birthday;
		//const addrInfo = e.detail.value;
		let addrInfo = {
			type:type,
			username: e.detail.value.username,
			phone: e.detail.value.phone,
			birthday: '',
			sex: '',
			gname: e.detail.value.gname,
			gaddress: e.detail.value.gaddress
		};
		addrInfo.sex = this.data.sex;
		addrInfo.birthday = this.data.birthday;
		
		if (userInfo.avatarUrl == '') {
			my.confirm({
				title: "提示",
				content: "请授权获取个人信息",
				showCancel: false
			});
		} else if (addrInfo.username == "" || addrInfo.phone == "" || addrInfo.birthday == "请选择出生日期") {
			this.data.isComplete = false;
			my.confirm({
				title: "提示",
				content: "请完善信息",
				showCancel: false
			});
		} else if (!/^[1][3,4,5,7,8]\d{9}$/.test(addrInfo.phone)) {
			my.confirm({
				title: "提示",
				content: "手机号格式不规范",
				showCancel: false
			});
		} else {
			if(type==1){
				if(addrInfo.gname =="" || addrInfo.gaddress ==""){
					my.confirm({
						title: "提示",
						content: "请完善信息",
						showCancel: false
					});
					return false;
				}
			}
			this.data.isComplete = true;
			my.setStorageSync({key:'addrInfo',data:addrInfo});
			that.updateAddr();
			return false;
		}
		this.setData({
			isDisabled: true,
			isComplete: this.data.isComplete
			// buttonTitle: "修改"
		});
	},
	updateAddr(e) {
		
		let that = this;
		//let userInfo = app.userInfo;
		let userInfo = my.getStorageSync({key:'userInfo'});
		let addrInfo = my.getStorageSync({key:'addrInfo'});
		let base_url = app.globalData.baseurl;
		let url = '/base/bind';
		let data = {
			'type':addrInfo.type,
			'imageUrl': userInfo.avatarUrl,
			'wechatName': userInfo.nickName,
			'openId': userInfo.openId,
			'username': addrInfo.username,
			'sex': addrInfo.sex,
			'birthday': addrInfo.birthday,
			'phoneNumber': addrInfo.phone,
			'companyName': addrInfo.gname,
			'companyAdress': addrInfo.gaddress
		}
		utils.request(userInfo, url, 'post', data, res => {
			if (res.data.status == 200) {
					let jsonuserinfo=res.data.data;
					userInfo.userId = jsonuserinfo.id;
					my.setStorageSync({key:'userInfo',data:userInfo});
					my.switchTab({
						'url': '../index/index'
					})
			} else if(res.data.status == 501){
				utils._login(res => {
					let jsonuserinfo = res.data.data;
					userInfo.openId = jsonuserinfo.openId;
					userInfo.headerValue = jsonuserinfo.sessionValue;
					userInfo.userId = jsonuserinfo.userId;
					my.setStorageSync({key:'userInfo',data:userInfo});
					that.updateAddr();
					
				});
			}
			return false;
		})

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		let that = this;
		let userInfo = my.getStorageSync({key:'userInfo'});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
	
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {
		
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {
		
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
